package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @version V1.0
 * @author:lxb
 * @Package
 * @date:2021/4/7
 * 通用：产品变更后已存在的产品包不能修改类型，只能修改个数
 */
public class AfterChangeNotEditAttrHandler  extends AbstractAttrHandler<KiteInst, AttrData> {
    @Autowired
    private IQuerySceneInstService querySceneInstService;
    @Override
    public String getValueDesc(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        if (KiteStringUtils.isEmpty(value)) {
            return value;
        }
        List<KiteAttrValue> attrValues = attr.getAttrValues();
        if (KiteListUtils.isEmpty(attrValues)) {
            return value;
        }
        String[] values = KiteStringUtils.split(value, SymbolConsts.COMMA);
        List<String> valueDesc = Arrays.stream(values).map(s -> {
            KiteAttrValue kiteAttrValue = attrValues.stream().filter(attrValue->
                KiteStringUtils.isEqual(attrValue.getAttrValue(), s)).findFirst().orElse(null);
            return Objects.nonNull(kiteAttrValue) ? kiteAttrValue.getAttrValueName() : "";
        }).collect(Collectors.toList());
        return KiteStringUtils.join(valueDesc, SymbolConsts.COMMA);
    }

    @Override
    public void onShow(KiteAttr attr, KiteInst inst, AttrData data) {
        if(KiteStringUtils.equalsAny(inst.getActionType().name(),KeyConsts.ACTION_TYPE_M,KeyConsts.ACTION_TYPE_K)){
            attr.setIsEdit(KeyConsts.IFTRUE_F);
        }else if(KiteStringUtils.equals(attr.getAttrValueTypeId(),"98K")){
            List<KiteAttrValue> attrValues = attr.getAttrValues();
            String sceneInstId = data.getSceneInstId();
            String tempDelId = querySceneInstService.getExtParameterValue(sceneInstId, LocalKeyConsts.TEMP_DEL_INST_ID);
            String tempInitId = querySceneInstService.getExtParameterValue(sceneInstId,LocalKeyConsts.TEMP_INIT_INST_ID);
            if(KiteStringUtils.equals(tempInitId,inst.getInstId())){
                String instId = inst.getInstId();
                String custId = data.getCustId();
                KiteProdInstFunc prodInstFunc = querySceneInstService.getProdInstFunc(data.getCustId(), instId);
                List<KiteProdInstFunc> prodFuncInstList = querySceneInstService.listProdInstFuncsByProdId(custId, prodInstFunc.getAccProdInstId(), prodInstFunc.getProdId());
                prodFuncInstList = prodFuncInstList.stream().filter(pdIt->!KiteStringUtils.equals(pdIt.getProdInstId(),tempInitId)&&(KiteStringUtils.isEmpty(tempDelId)||!tempDelId.contains(pdIt.getProdInstId()))).collect(Collectors.toList());
                if(KiteListUtils.isNotEmpty(prodFuncInstList)){
                    for(KiteAttrValue attrValue : attrValues){
                        if(prodFuncInstList.stream().anyMatch(pdit->KiteStringUtils.equals(pdit.getAttrValue(attr.getFieldName()),attrValue.getAttrValue()))){
                            if(KiteStringUtils.equals(attr.getValue(),attrValue.getAttrValue())||(KiteStringUtils.isEmpty(attr.getValue())&&KiteStringUtils.equals(attr.getDefaultValue(),attr.getValue()))){
                                attr.setValue(null);
                                attr.setValueDesc(null);
                                attr.setDefaultValue(null);
                            }
                            attrValue.setIsEdit("F");
                        }
                    }
                }
            }
        }
    }
}
